package view;

import java.awt.Color;
import java.awt.Graphics;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import model.Node;

public class GamePanel extends JPanel implements Observer {  //观察者GamePanel

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int WIDTH = 800;
	public static final int HEIGHT = 800;
	private LinkedList<Node> body;     //Node的集合体body
    private Node food;
	
	public GamePanel() {
		setSize(WIDTH, HEIGHT);
		setBackground(Color.gray);
		setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));  //设置边框类型
		

	}

	@Override
	public void paint(Graphics g) {     //paint图形方法,(图形画笔g) 

		// TODO Auto-generated method stub

		super.paint(g);

		if (body == null)
			return;
		Color color = Color.RED;
		for (Node n : body) {       //设置蛇的颜色
			g.setColor(color);
			g.fill3DRect(n.getX(), n.getY(), Node.WIDTH, Node.HEIGHT, true);    //画填充的正方形（参数）
			color = color.getRGB() == Color.RED.getRGB() ? Color.BLACK : Color.RED; //获取的颜色是红色的时候后面的颜色就是黑色，反之就变为红色   (条件？表达式1:表达式2)
		
		}
		if(food!=null){       //设置食物
			g.setColor(Color.green);
			g.fill3DRect(food.getX(), food.getY(),Node.WIDTH, Node.HEIGHT, true);	
		}
			
		

	}

	@SuppressWarnings("unchecked")
	@Override

	public void update(Observable o, Object arg) {    //被观察者o，被观察者所传递的数据（arg）
		
        if(arg instanceof Node){              //如果传过来的是Node类型(instanceof可以确定对象是否属于一个特定的类)
    	   food=(Node) arg;                
       }else if(arg instanceof List)        //如果传过来的是集合
       {
		body = (LinkedList<Node>) arg; 	      
       } 
        repaint(); //恢复原来的 
        
       }
		           
}
